package com.zretc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zretc.dao.CollectDao;
import com.zretc.entity.Collect;
import com.zretc.entity.Singer;
import com.zretc.entity.Song;
import com.zretc.util.DBUtil;
import com.zretc.util.PageInfo;

public class CollectDaoImpl implements CollectDao {

	@Override
	public int insertCollect(Map<String, Object> map) {
		int n = DBUtil.doUpdate("insert into collect values(?,?)", map.get("accountId"),map.get("songId"));
		return n;
	}

	@Override
	public int delectCollect(Map<String, Object> map) {
		int n = DBUtil.doUpdate("delete from collect where account_id=? and song_id=?",  map.get("accountId"),map.get("songId"));
		return n;
	}

	@Override
	public Collect selectCollectBySongId(Integer songId,Integer accountId) {
		Collect collect = null;
		ResultSet rs = DBUtil.doQuery("select account_id,song_id from collect where song_id = ? and account_id= ?", songId,accountId);
		if (rs!=null) {
			try {
				while (rs.next()) {
					collect = new Collect(accountId, songId);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return collect;
	}

	@Override
	public PageInfo<Collect> selectCollectByAccountId(Map<String, String> map) {
		PageInfo<Collect> pageInfo = null;
		Collect collect = null;
		Song song = null;
		Singer singer = null;
		List<Collect> data = new ArrayList<>();
		Integer accountId = Integer.parseInt(map.get("accountId"));
		Integer pageNum = Integer.parseInt(map.get("pageNum"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		ResultSet rs = DBUtil.doQuery("select A.account_id,B.song_id,B.song_name,B.song_time,B.click_count,B.song_text,B.song_status,B.type_id,C.singer_id,B.publish_date,"
								+ " C.singer_name,C.gender,C.age,C.singer_picture,C.area_id from collect A left join song B on A.song_id = B.song_id "
								+ " left join singer C on B.singer_id = C.singer_id where A.account_id = ? limit ?,?",accountId,(pageNum-1)*pageSize,pageSize);
		try {
			while (rs.next()) {
				Integer songId = rs.getInt("song_id");
				String songName = rs.getString("song_name");
				String songTime = rs.getString("song_time");
				Integer clickCount = rs.getInt("click_count");
				String songText = rs.getString("song_text");
				Integer songStatus = rs.getInt("song_status");
				Integer singerId = rs.getInt("singer_id");
				String publishDate = rs.getString("publish_date");
				String singerName = rs.getString("singer_name");
				String gender = rs.getString("gender");
				String singerPicture = rs.getString("singer_picture");
				singer = new Singer(singerId, singerName, gender, singerPicture, null);
				song = new Song(songId, songName, songTime, clickCount, songStatus, null, singer, songText, publishDate);
				collect = new Collect(accountId, song);
				data.add(collect);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		
		rs = DBUtil.doQuery("select count(*),account_id,song_id from collect where account_id = ?", accountId);
		
		try {
			while (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		
		return pageInfo;
	}

}
